package com.tongji.housediy.view.titlewindows
{
	import com.tongji.housediy.events.UserLoadandRegisterEvent.LoginEvent;
	import com.tongji.housediy.events.UserLoadandRegisterEvent.UserRegisterEvent;
	import com.tongji.housediy.events.houseEvent.OpenHouseEvent;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.utils.PopUpAdmin;
	import com.tongji.housediy.view.titlewindows.RegisterTitleWindow;
	
	import flash.events.MouseEvent;
	import flash.net.SharedObject;
	import flash.ui.Mouse;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author songjia
	 * 2011/5/11
	 */ 
	public class LoginTitleWinMediator extends Mediator
	{
		[Inject]
		public var view:LoginTitleWindow;
		
		[Inject]
		public var userModel:UserInforModel;
		
		public var sharedObject:SharedObject;
		
		public function LoginTitleWinMediator()
		{
			super();
		}
		
		public override function onRegister():void
		{
			sharedObject = SharedObject.getLocal("User");
			if(sharedObject.data.isAuotoLogin)
			{
				view.userName.text = sharedObject.data.userName;
				view.password.text = sharedObject.data.password;
				view.isAutoLogin.selected = sharedObject.data.isAuotoLogin;
			}
			view.closeBtn.addEventListener(MouseEvent.CLICK, closeBtnHandler);
			view.registerBtn.addEventListener(MouseEvent.CLICK, registerBtnHandler);
			view.loginBtn.addEventListener(MouseEvent.CLICK, loginBtnHandler);
			view.forgetCode.addEventListener(MouseEvent.CLICK, findCodeBtnHandler);
		}
		
		public function closeBtnHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);	
		}
		
		public function registerBtnHandler(event:MouseEvent):void
		{			
			var creatNewAccount:RegisterTitleWindow = new RegisterTitleWindow();
			PopUpManager.addPopUp(creatNewAccount, view.parent, true);
			PopUpManager.centerPopUp(creatNewAccount);
			mediatorMap.createMediator(creatNewAccount);
			PopUpManager.removePopUp(view);
		}
		public function findCodeBtnHandler(Event:MouseEvent):void
		{
			var creatForgetCode:FindPassWordTitleWindow =new FindPassWordTitleWindow();
			PopUpManager.addPopUp(creatForgetCode, view.parent, true);
			PopUpManager.centerPopUp(creatForgetCode);
			mediatorMap.createMediator(creatForgetCode);
			PopUpManager.removePopUp(view);
		}
		
		public function loginBtnHandler(event:MouseEvent):void
		{
			sharedObject = SharedObject.getLocal("User");
			sharedObject.data.userName = view.userName.text;
			sharedObject.data.password = view.password.text;
			sharedObject.data.isAuotoLogin = view.isAutoLogin.selected;
			
			userModel.userName = view.userName.text;
			this.dispatch(new LoginEvent(LoginEvent.USER_LOGIN, view.userName.text, view.password.text));
			this.addContextListener(LoginEvent.UPDATE_USER, openDesignModel, LoginEvent);
			//this.addContextListener(OpenHouseEvent.UPDATE_HOUSE_MODEL, removeWin, OpenHouseEvent);						
		}
		
		public function removeWin(event:OpenHouseEvent):void
		{
			
		}
		
		private function openDesignModel(event:LoginEvent):void
		{	
			switch(view.name)
			{
				case "login":
					break;
				case "openDesign":					
					PopUpAdmin.popupCenterWindow("openDesignTitle", view.parent, mediatorMap);					
					break;
				case "saveDesign":
					PopUpAdmin.popupCenterWindow("saveDesignTitle", view.parent, mediatorMap);
					break;					
			}
			view.name = "";
			PopUpManager.removePopUp(view);				
		}
		
	}
}